/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.adapter;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class BPODatabase<T extends BaseProxyObject> {
    private Map<xowl.lang.owl2.Ontology, Map<xowl.lang.runtime.Entity, T>> database;
    
    public BPODatabase() {
        database = new HashMap<xowl.lang.owl2.Ontology, Map<xowl.lang.runtime.Entity, T>>();
    }
    
    public void insert(T bpo) {
        Map<xowl.lang.runtime.Entity, T> sub = database.get(bpo.getOntology());
        if (sub == null) {
            sub = new HashMap<xowl.lang.runtime.Entity, T>();
            database.put(bpo.getOntology(), sub);
        }
        sub.put(bpo.getEntity(), bpo);
    }
    
    public void remove(T bpo) {
        Map<xowl.lang.runtime.Entity, T> sub = database.get(bpo.getOntology());
        if (sub == null)
            return;
        sub.remove(bpo.getEntity());
    }
    
    public T get(xowl.lang.runtime.Entity entity) {
        xowl.lang.owl2.Ontology onto = entity.getContainedBy();
        Map<xowl.lang.runtime.Entity, T> sub = database.get(onto);
        if (sub == null)
            return null;
        return sub.get(entity);
    }
}
